package com.sxfq.loansupermarket.backsystem.modular.weixin.controller;

import com.sxfq.loansupermarket.backsystem.GunsApplication;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.weixin.config.WebChatConfig;
import com.sxfq.loansupermarket.backsystem.modular.weixin.entity.WxTokenInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sxfq.loansupermarket.backsystem.modular.weixin.service.IWxService;

/**
 * <pre>
 * @Desc: 测试生成带参数二维码,   2592000(有效期30天)
 * @Package: com.thinkgem.jeesite.modules.wechat.controller
 * @Author: cao_wencao
 * @Date: 2019-04-08 10:30
 * </pre>
 */
@Controller
@RequestMapping("/wechat/qrcode")
public class TestQrcodeController {
    @Autowired
    private IWxService wxService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    protected final static Logger log = LoggerFactory.getLogger(GunsApplication.class);

    /**
     * <pre>
     * @desc: 创建生成二维码
     * @auth: cao_wencao
     * @date: 2019/4/10 14:00
     * </pre>
     */
    @RequestMapping("/createQrcode")
    @ResponseBody
    public Object createQrcode() {
        WeixinController weixinController = new WeixinController();
        String access_token = weixinController.getAccessToken();
        com.alibaba.fastjson.JSONObject jsonObject = wxService.qrCodeCreateTmpTicket(access_token, "");
        return jsonObject;
    }

    /**
     * <pre>
     * @desc: 通过ticket获取二维码（长链接URL）
     * @auth: cao_wencao
     * @date: 2019/4/10 14:00
     * </pre>
     */
    @RequestMapping("/getQrcodeUrl")
    @ResponseBody
    public Object getQrcodeUrl() {
        String ticket = "";
        String access_token = getAccessToken();
        com.alibaba.fastjson.JSONObject jsonObject = wxService.qrCodeCreateTmpTicket(access_token, "");
        if (null != jsonObject) {
            ticket = jsonObject.getString("ticket");
        }
        String url = wxService.qrCodePictureUrl(ticket);
        return url;
    }

    public String getAccessToken() {
        String accessToken = "";
        String getTokenSql = "select access_token as accessToken from wx_parameter_info where app_id='" + redisTemplate.opsForList().index("APPID",0) + "' limit 1";
        WxTokenInfo wxTokenInfo = sqlMapper.selectOne(getTokenSql, WxTokenInfo.class);
        if (wxTokenInfo == null) {
            accessToken = WebChatConfig.getToken(redisTemplate.opsForList().index("APPID",0)+"",redisTemplate.opsForList().index("AppSecret",0)+"").getAccessToken();
            String updTokenSql = "update wx_parameter_info set access_token='" + accessToken + "' where app_id='" + redisTemplate.opsForList().index("APPID",0) + "';";
            sqlMapper.update(updTokenSql);
        } else {
            if ("".equals(wxTokenInfo.getAccessToken()) || "null".equals(wxTokenInfo.getAccessToken())) {
                accessToken = WebChatConfig.getToken(redisTemplate.opsForList().index("APPID",0)+"",redisTemplate.opsForList().index("AppSecret",0)+"").getAccessToken();
                String updTokenSql = "update wx_parameter_info set access_token='" + accessToken + "' where app_id='" + redisTemplate.opsForList().index("APPID",0) + "';";
                sqlMapper.update(updTokenSql);
            }
            accessToken = wxTokenInfo.getAccessToken();
        }
        return accessToken;
    }

//    /**
//     * <pre>
//     * @desc: 通过ticket获取二维码（短链接URL）
//     * @auth: cao_wencao
//     * @date: 2019/4/10 14:01
//     * </pre>
//     */
//    @RequestMapping("/qrCodePictureUrl")
//    @ResponseBody
//    public Object qrCodePictureUrl(String ticket){
//        String urlPicture = wxService.qrCodePictureUrl(ticket,true);
//        // String url= urlPicture.replace("\\/", "/");
//        return urlPicture;
//    }
}